package org.orz.story.pms.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.orz.cloud.auth.UserUtil;
import org.orz.cloud.auth.domain.UserInfo;
import org.orz.cloud.common.enums.AliveFlagEnum;
import org.orz.story.pms.api.enums.WorkbenchCodeEnum;
import org.orz.story.pms.api.model.dto.CreatePmsAccountReq;
import org.orz.story.pms.biz.persistence.entities.PmsAccount;
import org.orz.story.pms.biz.persistence.entities.PmsAccountWorkbench;
import org.orz.story.pms.biz.persistence.entities.PmsWorkbench;
import org.orz.story.pms.biz.persistence.mappers.PmsAccountMapper;
import org.orz.story.pms.biz.persistence.mappers.PmsAccountWorkbenchMapper;
import org.orz.story.pms.biz.persistence.mappers.PmsWorkbenchMapper;
import org.orz.story.pms.biz.service.PmsAccountService;
import org.orz.story.pms.common.exception.PmsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class PmsAccountServiceImpl implements PmsAccountService {
    @Autowired
    private PmsAccountMapper pmsAccountMapper;
    @Autowired
    private PmsAccountWorkbenchMapper pmsAccountWorkbenchMapper;
    @Autowired
    private PmsWorkbenchMapper pmsWorkbenchMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createPmsAccount(CreatePmsAccountReq req) {
        // 1.创建权限账号
        UserInfo userInfo = UserUtil.getUserInfo();
        PmsAccount pmsAccount = new PmsAccount();
        pmsAccount.setMemAccountId(req.getMemAccountId());
        pmsAccount.setCreateUser(userInfo == null ? 0 : userInfo.getAccountId());
        pmsAccount.setCreateDate(new Date());
        pmsAccountMapper.insert(pmsAccount);

        // 2.关联工作台
        if(CollUtil.isEmpty(req.getWorkbenchCode())){
            throw new PmsException(PmsException.Type.PMS_0006);
        }
        Set<String> workbenchCodeSet = req.getWorkbenchCode().stream().map(WorkbenchCodeEnum::getCode).collect(Collectors.toSet());
        List<PmsWorkbench> workbenchList = pmsWorkbenchMapper.selectList(new LambdaQueryWrapper<PmsWorkbench>()
                .in(PmsWorkbench::getWorkbenchCode, workbenchCodeSet)
                .eq(PmsWorkbench::getAliveFlag, AliveFlagEnum.ALIVE.getKey()));
        if(CollUtil.isEmpty(workbenchList)){
            throw new PmsException(PmsException.Type.PMS_0005);
        }
        workbenchList.forEach(item -> {
            PmsAccountWorkbench accountWorkbench = new PmsAccountWorkbench();
            accountWorkbench.setAccountId(pmsAccount.getId());
            accountWorkbench.setWorkbenchId(item.getId());
            accountWorkbench.setCreateUser(userInfo == null ? 0 : userInfo.getAccountId());
            accountWorkbench.setCreateDate(new Date());
            pmsAccountWorkbenchMapper.insert(accountWorkbench);
        });
    }
}
